﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GraphicLib;
using System.IO;

namespace GraphicIdentification
{
    public partial class frmGrapicIdentification : Form
    {
        public frmGrapicIdentification()
        {
            InitializeComponent();
            //界面上有个初始化按钮，比较值，输出
            SamplesInition.DeserializeSample();
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            if (!HasSelectProduct())
            {
                MessageBox.Show("请先选择产品!");
                return;
            }
            try
            {
                txtLog.AppendText(string.Format("{0} 开始初始化样本图像...\r\n",DateTime.Now));
                SamplesInition.InitSamples();
                txtLog.AppendText(string.Format("{0} 初始化样本图像完成!\r\n",DateTime.Now));
            }
            catch (Exception ex)
            {
                txtLog.AppendText(ex.Message);
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!HasSelectProduct())
            {
                MessageBox.Show("请先选择产品!");
                return;
            }
            ClearPrevResult();
            try
            {
                //坏品自学习
                bad_product_learning();
                //检测产品，并输出日志                
                txtLog.AppendText(string.Format("---- {0} 开始产品检测 ----\r\n", DateTime.Now));

                //ProductCheck.CheckProducts(txtLog);
                ProcessThread processTread = new ProcessThread();
                processTread.StartAProcessThread(txtLog, btnStart, pictureBoxProduct, panelResultColor);
                btnStart.Enabled = false;
                btnInit.Enabled = true;
            }
            catch (Exception ex)
            {
                txtLog.AppendText(ex.Message);
            }
        }
        /// <summary>
        /// 坏品学习增加切割次数
        /// </summary>
        private void bad_product_learning()
        {
            if (!HasSelectProduct())
            {
                MessageBox.Show("请先选择产品!");
                return;
            }
            string processFolder = Sample.BAD_PRODUCTS_LEARNING_DIR;
            //string badProductFilePath = Sample.BAD_PRODUCT_DIR;
            string[] files = Directory.GetFiles(processFolder);
            if (files.Length>0)
            {
                foreach (string file in files)
                {
                    try
                    {
                        Image img = Image.FromFile(file);
                        img.Dispose();
                        ProductCheck.SPLIT_COUNT += 1;
                        frmSetting.SerializeParameters();
                        SamplesInition.ReCutInit();
                        //string fileName = file.Substring(file.LastIndexOf(@"\") + 1);
                        File.Delete(file);
                        //File.Move(file, badProductFilePath + "\\"+fileName);                      
                    }
                    catch (Exception ex)
                    {
                        File.AppendAllText(Sample.LOG_PATH, ex.Message);
                    }
                }
            }
        }

        private void btnSampleLearn_Click(object sender, EventArgs e)
        {
            if (!HasSelectProduct())
            {
                MessageBox.Show("请先选择产品!");
                return;
            }
            txtLog.AppendText(string.Format("{0} 开始样本学习...\r\n", DateTime.Now));
            SamplesInition.LearnNewSamples();
            txtLog.AppendText(string.Format("{0} 样本学习完成!\r\n", DateTime.Now));
        }

        private void paramsSetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSetting setting = new frmSetting();
            setting.ShowDialog();
        }

        private void productsManageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmProductsManage productsManage = new frmProductsManage();
            productsManage.ShowDialog();
            if (productsManage.DialogResult == DialogResult.OK)
            {
                lblCurrentProduct.Text = string.Format("当前选择产品：{0}", Sample.SELECTED_PRODUCT);
            }
        }

        /// <summary>
        /// 是否已选择产品
        /// </summary>
        /// <returns></returns>
        private bool HasSelectProduct()
        {
            if (string.IsNullOrWhiteSpace(Sample.SELECTED_PRODUCT))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 清除前一次检测结果
        /// </summary>
        private void ClearPrevResult()
        {
            panelResultColor.BackColor = SystemColors.Control;
            pictureBoxProduct.Image = null;
            DeletePrevImage();
        }

        /// <summary>
        /// 删除前一次检测的图片
        /// </summary>
        private void DeletePrevImage()
        {
            if (!string.IsNullOrWhiteSpace(Sample.CURRENT_PRODUCT_IMAGE_PATH) && File.Exists(Sample.CURRENT_PRODUCT_IMAGE_PATH))
            {
                File.Delete(Sample.CURRENT_PRODUCT_IMAGE_PATH);
                Sample.CURRENT_PRODUCT_IMAGE_PATH = null;
            }
        }

        private void btnGoodProduct_Click(object sender, EventArgs e)
        {

            FileUtils.MoveSingleFile(Sample.CURRENT_PRODUCT_IMAGE_PATH, Sample.NEW_SAMPLE_DIR);
            txtLog.AppendText(string.Format("{0} 开始好品学习...\r\n", DateTime.Now));
            SamplesInition.LearnNewSamples();
            txtLog.AppendText(string.Format("{0} 好品学习完成!\r\n", DateTime.Now));
            ClearPrevResult();
        }

        private void btnBadProduct_Click(object sender, EventArgs e)
        {
            FileUtils.MoveSingleFile(Sample.CURRENT_PRODUCT_IMAGE_PATH, Sample.BAD_PRODUCTS_LEARNING_DIR);
            txtLog.AppendText(string.Format("{0} 开始坏品学习...\r\n", DateTime.Now));
            bad_product_learning();
            txtLog.AppendText(string.Format("{0} 坏品学习完成!\r\n", DateTime.Now));
            ClearPrevResult();
        }

    }
}
